<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <!-- 栅格布局 搜索新增开始 -->
        <div class="app-containers">
          <el-row :gutter="20">
            <el-col :span="3">请输入关键词 </el-col>
            <el-col :span="4">
              <div class="grid-content bg-purple">
                <el-input v-model="questionsName" placeholder="根据编号搜索" />
              </div>
            </el-col>
            <el-col :span="10">
              <div class="grid-content bg-purple sqcs">
                <el-button @click="clearFn()">清除</el-button>
                <el-button type="primary" @click="searchFn()">搜索</el-button>
              </div>
            </el-col>
          </el-row>
        </div>
        <!-- 栅格布局 搜索新增结束 -->
        <el-alert
          :title="`数据一共${counts}条`"
          type="info"
          show-icon
          :closable="false"
        />
        <!-- 渲染数据 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            label="编号"
            type="index"
            :index="indexMethod"
            sortable=""
          />
          <el-table-column
            prop="questionType"
            label="题型"
            width="100px"
            :formatter="formOfEmploymentEmploymentFn"
          />
          <el-table-column prop="id" label="题目编号" width="190px">
            <template #default="{ row }">
              <p class="xqcs" @click="xqqsFn(row.id)">{{ row.id }}</p>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间">
            <template #default="{ row }">
              {{ row.addDate | formatTime('YYYY-MM-DD HH:MM:ss') }}
            </template>
          </el-table-column>
          <el-table-column
            prop="totalSeconds"
            label="答题时间(s)"
            width="100px"
          />
          <el-table-column
            prop="questionType"
            label="正确率(%)"
            width="100px"
          />
          <el-table-column prop="userName" label="录入人" width="80px" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                plain
                @click="delFn(row.id)"
              />
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件开始 -->
        <el-row>
          <el-col :span="12"><div class="grid-content bg-purple" /></el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <div class="fycs">
                <el-pagination
                  :current-page="currentPage4"
                  :page-sizes="[5, 10, 15, 20]"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="counts"
                  :page-count="counts * 0"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                />
              </div>
            </div>
          </el-col>
        </el-row>

        <!-- 分页组件结束 -->
      </el-card>
      <!-- 新增编辑的弹层 -->
      <!-- <questions-preview
        v-if="dialogVisible"
        :dialog-visible="dialogVisible"
        :question-item="questionItem"
        @close="dialogVisible = false"
      /> -->
      <previewDialog :id="id" :show-dialog.sync="showDialog" />
    </div>
  </div>
</template>

<script>
import {
  getQuestionsApi,
  getDelquestionsApi,
  getSearchQuestionsApi
} from '@/api/questions'
import { difficulty } from '@/api/constants/constants'
// import questionsPreview from './components/questions-preview.vue'
import previewDialog from '@/views/questions/basic/components/previewDialog.vue'
export default {
  components: {
    // questionsPreview
    previewDialog
  },
  data() {
    return {
      questionsName: '', // 组题搜索
      loading: false,
      counts: 0,
      page: 1,
      pagesize: 10,
      currentPage4: 1, // 分页组件
      tableData: [], // 表格假数据
      showDialog: false,
      id: ''
    }
  },

  created() {
    this.getQuestionsApi() // 获取当前页数据
  },
  methods: {
    // 题目详情{}xqqsFn
    async xqqsFn(id) {
      this.showDialog = true
      this.id = id
      // try {
      //   const { data } = await getQuestionsxqApi(id)
      //   this.questionItem = data
      //   this.showDialog = true
      // } catch (error) {
      //   this.$message.info('数据未找到')
      //   return error
      // }
    },
    // 处理题型问题
    formOfEmploymentEmploymentFn(row, column, cellValue, index) {
      // find 查到到符合的元素并且返回
      const result = difficulty.find((item) => item.value === +cellValue)
      return result ? result.label : '未知'
    },
    // clearF 清除
    clearFn() {
      this.questionsName = ''
      this.getQuestionsApi()
    },
    // 搜索
    async searchFn() {
      this.loading = true // 设置加载等待动画
      const data = await getSearchQuestionsApi(this.questionsName)
      this.tableData = data.items
      this.counts = data.counts
      if (this.counts === 0) {
        this.$message.error('没有当前题目')
      }
      this.loading = false
    },
    // 获取当前页数据
    async getQuestionsApi() {
      this.loading = true // 设置加载等待动画
      const res = await getQuestionsApi(this.page, this.pagesize)
      this.counts = res.counts
      this.tableData = res.items
      this.loading = false
    },
    // 页码数据改变时
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.getQuestionsApi() // 数据发生改变重新渲染数据
    },
    // 页面变动的数据
    handleCurrentChange(val) {
      this.page = val
      this.getQuestionsApi() // 数据发生改变重新渲染数据
    },
    // 处理序号问题 根据index的变化 进
    indexMethod(index) {
      // 当前返回的序号 前面有多少条数据
      // 前面一共有多少条数据 前面的多少页 * 每页的数据的条数 + index +1
      return (this.page - 1) * this.pagesize + index + 1
    }, // 删除数据
    delFn(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await getDelquestionsApi(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getQuestionsApi()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }, // 关闭弹层
    closeFn() {
      this.showDialog = false
    }
  }
}
</script>
<style lang="scss" scoped>
.sqcs {
  float: right;
}
.app-containers {
  margin-bottom: 15px;
}
::v-deep .el-table thead {
  color: #909399;
  font-weight: 500;
  background-color: #909399;
}
.fycs {
  margin-top: 15px;
}
::v-deep.el-dialog__header {
  background: -webkit-gradient(
    linear,
    left top,
    right top,
    from(#1493fa),
    to(#01c6fa)
  );
  background: linear-gradient(90deg, #1493fa, #01c6fa);
  background: #409eff;
  border-radius: 9px 9px 0 0;
}
.xqcs {
  color: #409eff;
  cursor: pointer;
}
</style>
